class code4 {
    //摩尔投票法
    public int majorityElement(int[] nums) {
        int ret = nums[0];
        int count = 1;

        for(int i=1;i<nums.length;i++) {
            int x = nums[i];
            if(x == ret) {
                count++;
            } else {
                count--;
                if(count == 0) {
                    ret = x;
                    count = 1;
                }
            }
        }
        return ret;
    }
}